package com.neu.controller;

import com.neu.pojo.Room;
import com.neu.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 获取房间信息
     */
    @RequestMapping("getRoomList")
    @ResponseBody
    public HashMap getRoomList(){
        List<Room> list=roomService.getRoomList();
        HashMap<String,Object> map =new HashMap<>();
        map.put("code",0);
        map.put("list",list);
        return map;
    }

    /**
     * 根据房间号删除房间
     * @param roomNum
     * @return
     */
    @RequestMapping("delRoom")
    @ResponseBody
    public HashMap delRoom(int roomNum){
        int res=roomService.delRoom(roomNum);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","删除成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    /**
     * 根据房间号更新房间的全部信息
     * @param room
     * @return
     */
    @RequestMapping("updateRoom")
    @ResponseBody
    public HashMap updateRoom(Room room){
        int res=roomService.updateRoom(room);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","更改成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","更改失败");
        }
        return map;
    }

    /**
     * 新增房间
     */
    @RequestMapping("addRoom")
    @ResponseBody
    public HashMap addRoom(Room room){
        int res=roomService.addRoom(room);
        HashMap<String,Object> map=new HashMap<>();
        if(res>0){
            map.put("code",0);
            map.put("msg","添加成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }


    /**
     * 按页获取房间信息
     * @param page 页号
     * @param limit 每页的数目
     * @return
     */
    @RequestMapping("getRoomByPage")
    @ResponseBody
    public HashMap getRoomByPage(int page, int limit){
        List<Room> list=roomService.getRoomByPage(page,limit);
        int totalCount=roomService.getRoomCount();
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }

    /**
     * 根据房间号获取房间
     * @param roomNum
     * @return
     */
    @RequestMapping("getRoomByRoomNum")
    @ResponseBody
    public HashMap getRoomByRoomNum(int roomNum){
        Room room=roomService.getRoomByRoomNum(roomNum);
        HashMap<String,Object> map=new HashMap<>();
        if(room!=null){
            map.put("code",0);
            map.put("data",room);
        }
        else{
            map.put("code",-1);
            map.put("data",null);
        }
        return map;
    }

}

